<template>
	<view class="applyClub">
		<view class="title">申请社团</view>
		<view class="container">
			<view class="clubName">
				<text>社团名称</text>
				<input type="text" placeholder="请输入社团名称" v-model="formData.clubName">
			</view>
			<view class="creater">
				<text>创办人</text>
				<input type="text" placeholder="请输入创办人" v-model="formData.founder">
			</view>
			<view class="responser">
				<text>负责人</text>
				<input type="text" placeholder="请输入负责人" v-model="formData.head">
			</view>
			<view class="responserPhoneNum">
				<text>负责人联系方式</text>
				<input type="number" placeholder="请输入负责人联系方式" v-model="formData.headPhone">
			</view>
			<view class="intro">
				<text>介绍</text>
				<textarea placeholder="为您的社团写一段介绍吧!" auto-height v-model="formData.clubIntro"
					:maxlength="200"></textarea>
			</view>
			<view class="logo">
				<text>社团logo</text>
				<view class="bthAddLogo" @click="handleImgChoose" v-if="!formData.clubLogo">
					+
				</view>
				<view class="conLogo" v-else>
					<view class="bthDeleteLogo" @click="handleDeleteLogo">
						×
					</view>
					<image :src="formData.clubLogo" mode="aspectFill"></image>
				</view>
			</view>
		</view>
		<view class="bthHandleApply" @click="handlleApply">
			<span v-if="ifEdit">编辑信息</span>
			<span v-else>+申请创建</span>
		</view>
	</view>
</template>

<script setup>
import {
	reactive,
	ref
} from 'vue'
import {
	applyClub,
	updateClub
} from '@/apis/apis';
import {
	onLoad
} from "@dcloudio/uni-app"
const ifEdit = ref(false)
onLoad((options) => {
	console.log(options)
	if (options.clubInfo) {
		uni.setNavigationBarTitle({
			title: '编辑信息'
		});
		ifEdit.value = true
		const clubInfo = JSON.parse(options.clubInfo)
		console.log(clubInfo, 'lll')
		setFormData(clubInfo)
	}
})
const formData = reactive({
	clubName: null,
	clubIntro: null,
	clubLogo: null,
	head: null,
	headPhone: null,
	founder: null
})
const setFormData = ({
	clubName,
	clubIntro,
	clubLogo,
	head,
	headPhone,
	founder,
	id
}) => {
	formData.clubName = clubName
	formData.clubIntro = clubIntro
	formData.clubLogo = clubLogo
	formData.head = head
	formData.headPhone = headPhone
	formData.founder = founder
	formData.clubId = id
}
const handlleApply = () => {
	for (const Key in formData)
		if (!formData[Key])
			return uni.showToast({
				title: '所有内容都不能为空!',
				icon: 'none'
			})
	if (!ifEdit.value) {
		applyClub(formData).then(res => {
			console.log(res)
			if (res.status === 200) {
				uni.showToast({
					title: '申请成功',
					icon: "success"
				})
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
				}, 500)
			}
		})
	}
	else {
		updateClub(formData).then(res => {
			console.log(res)
			if (res.status === 200) {
				uni.showToast({
					title: '编辑成功',
					icon: "success"
				})
				setTimeout(() => {
					uni.navigateBack({
						delta: 1
					})
				}, 500)
			}
		})
	}
}
const handleImgChoose = () => {
	const apiUrl = 'https://tww.jyu.edu.cn'
	// const apiUrl = 'https://jyutw.alcy.top'
	uni.chooseImage({
		count: 1,
		sizeType: ['original', 'compressed'],
		sourceType: ['album', 'camera'],
		success: (res) => {
			formData.clubLogo = res.tempFilePaths[0];
			uni.uploadFile({
				url: `${apiUrl}/apis/uploadFiles`,
				filePath: formData.clubLogo,
				header: {
					'Authorization': 'Bearer ' + uni.getStorageSync('jyu_token') ||
						useUser
							.token || ''
				},
				name: 'file',
				timeout: 10000,
				success: res => {
					formData.clubLogo = apiUrl + JSON.parse(res.data)
						.filePaths[0]
				},
				fail: err => {
					console.log(err)
				}
			})
		},
		fail: (err) => {
			console.error(err);
		}
	});
}
const handleDeleteLogo = () => {
	uni.showModal({
		title: '提示',
		content: '确定删除吗?',
		success: (res) => {
			if (res.confirm) {
				formData.clubLogo = null
				uni.showToast({
					title: '删除成功',
					icon: 'none'
				});
			} else if (res.cancel) {
				uni.showToast({
					title: '取消删除',
					icon: 'none'
				});
			}
		}
	});
}
</script>

<style lang="less" scoped>
.applyClub {
	background-color: #f1f1f1;
	padding-bottom: 40rpx;

	.title {
		height: 100rpx;
		font-size: 34rpx;
		color: gray;
		margin-left: 30rpx;
		line-height: 100rpx;
	}

	.container {
		background-color: #fff;
		border-radius: 20rpx;
		width: 95%;
		margin: 0 auto;
		padding-bottom: 40rpx;

		.clubName,
		.creater,
		.responser,
		.responserPhoneNum,
		.intro,
		.logo {
			display: flex;
			flex-direction: column;

			text {
				height: 100rpx;
				line-height: 100rpx;
				font-size: 28rpx;
				font-weight: 550;
				margin-left: 20rpx;
			}

			input,
			textarea {
				width: 85%;
				margin: 0 auto;
				height: 80rpx;
				background-color: #f7f7f7;
				padding: 0 20rpx;
				border-radius: 20rpx;
			}
		}

		.intro {
			textarea {
				padding-top: 20rpx;
				min-height: 80rpx;
				max-height: 200rpx;
			}
		}

		.logo {

			.bthAddLogo,
			.conLogo {
				height: 200rpx;
				width: 200rpx;
				background-color: #f7f7f7;
				color: grey;
				margin-left: 30rpx;
				border-radius: 20rpx;
				font-size: 70rpx;
				text-align: center;
				line-height: 200rpx;
				position: relative;
				overflow: hidden;

				image {
					height: 100%;
					width: 100%;
				}

				.bthDeleteLogo {
					width: 50rpx;
					height: 50rpx;
					background-color: black;
					line-height: 50rpx;
					font-size: 30rpx;
					position: absolute;
					top: -5rpx;
					right: -10rpx;
					text-align: center;
					opacity: .4;
					border-radius: 0 0 0 50rpx;
					color: #fff;
				}
			}
		}
	}

	.bthHandleApply {
		width: 90%;
		margin: 30rpx auto;
		height: 100rpx;
		line-height: 100rpx;
		background: linear-gradient(146deg, #C9FF99 0%, #00E3AE 100%);
		font-size: 34rpx;
		text-align: center;
		font-weight: 550;
		color: #fff;
	}
}
</style>